package com.fsh.anime.controller;

import com.fsh.anime.model.dtos.EmailLoginDto;
import com.fsh.anime.model.dtos.EmailRegistryDto;
import com.fsh.anime.model.dtos.PageUserDto;
import com.fsh.anime.model.dtos.UserMarkingDto;
import com.fsh.anime.model.pojos.User;
import com.fsh.anime.model.vos.Result;
import com.fsh.anime.service.UserService;
import com.fsh.anime.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/api/users")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 获取分页用户列表
     *
     * @param dto dto
     * @return {@link Result}
     */
    @GetMapping("/getUsers")
    public Result getUsers(PageUserDto dto){
        log.info("PageUserDto,{}",dto);
        return userService.getUsers(dto);
    }

    /**
     * 添加用户
     *
     * @param user 用户
     * @return {@link Result}
     */
    @PostMapping("/addUser")
    public Result addUser(@RequestBody User user){
        log.info("addUser,{}",user);
        return userService.addUser(user);
    }

    /**
     * 更新用户状态
     *
     * @param id     id
     * @param status 状态
     * @return {@link Result}
     */
    @PutMapping("/updateUserStatus")
    public Result updateUserStatus(Integer id,Integer status){
        return userService.updateUserStatus(id,status);
    }


    /**
     * 删除用户
     *
     * @param id id
     * @return {@link Result}
     */
    @DeleteMapping("/delUser/{id}")
    public Result delUser(@PathVariable Integer id){
        return userService.delUser(id);
    }


//--------------用户界面网站---------
    /**
     * 邮箱密码用户登录
     *
     * @param dto dto
     * @return {@link Result}
     */
    @PostMapping("/emailLogin")
    public Result emailLogin(@RequestBody @Validated EmailLoginDto dto,BindingResult results ){
        if (results.hasErrors()){
            return Result.error(results.getFieldError().getDefaultMessage());
        }
        return userService.emailLogin(dto);
    }

    /**
     * 发送邮箱验证码
     */
    @GetMapping("/sendEmailCode")
    public Result sendEmailCode(String email){
        if (StringUtils.isBlank(email)){
            return Result.error("邮箱不能为空");
        }
        return userService.sendEmailCode(email);
    }


    /**
     * 邮箱注册
     */
    @PostMapping("/emailRegister")
    public Result emailRegister(@RequestBody @Validated EmailRegistryDto dto, BindingResult results){
        if (results.hasErrors()){
            return Result.error(results.getFieldError().getDefaultMessage());
        }
        return userService.emailRegister(dto);
    }

    /**
     * 获取微信扫码生成的token
     */
    @GetMapping("/wx/getWxToken")
    public String getWxToken(){
        String token = stringRedisTemplate.opsForValue().get("wx:token");
        if (Objects.isNull(token)){
            return "no_token";
        }
        //删除token
        stringRedisTemplate.delete("wx:token");
        return token;
    }


    /**
     * 得到分数
     *
     * @param id id
     * @return {@link Result}
     */
    @GetMapping("/getScore")
    public Result getScore(Integer id){
//        log.info("该番剧id,{}。当前登录用户id为：{}",id, BaseContext.getCurrentId());
        return userService.getScore(id);
    }

    /**
     * 当前登录用户 番剧打分
     *
     * @param dto dto
     * @return {@link Result}
     */
    @PostMapping("/userMarking")
    public Result userMarking(@RequestBody UserMarkingDto dto){
//        log.info("userMarkingDto,{}",dto);
        return userService.userMarking(dto);
    }

    /**
     * 关注作者
     *
     * @param followId  被关注作者的id
     * @param followsUp 关注行为
     * @return {@link Result}
     */
    @PostMapping("/follow/{followId}/{followsUp}")
    public Result follow(@PathVariable Long followId,@PathVariable Integer followsUp){
//        log.info("{},{}",followId,followsUp);
//        return null;
        return userService.follow(followId,followsUp);
    }

    /**
     * 删除 当前用户的文章
     */
    @DeleteMapping("/delArticle/{id}")
    public Result delArticle(@PathVariable String id){
        return userService.delArticle(id);
    }

}
